import matplotlib.pyplot as plt

# 绘制结果
def plot_results(history):
    plt.figure(figsize=(10, 6))
    plt.plot(history)
    plt.title("Evolution of Best Distance")
    plt.xlabel("Generation")
    plt.ylabel("Distance")
    plt.grid(True)
    plt.show()

# 绘制路径图
def plot_path(cities, path, title="TSP Path"):
    x = [city[0] for city in cities]
    y = [city[1] for city in cities]
    
    plt.figure(figsize=(10, 6))
    
    # 绘制城市点
    plt.scatter(x, y, c='red', s=100)
    
    # 绘制路径
    path_x = [x[i] for i in path] + [x[path[0]]]
    path_y = [y[i] for i in path] + [y[path[0]]]
    plt.plot(path_x, path_y, 'b-')
    
    # 标记起点
    plt.scatter(x[path[0]], y[path[0]], c='green', s=150, marker='*', label='Start')
    
    # 添加标签
    for i, (xi, yi) in enumerate(zip(x, y)):
        plt.annotate(f"{i}", (xi, yi), fontsize=12)
    
    plt.title(title)
    plt.xlabel("X Coordinate")
    plt.ylabel("Y Coordinate")
    plt.legend()
    plt.grid(True)
    plt.show()